package com.javaweb.service.impl;

import com.javaweb.aop.GranularType;
import com.javaweb.aop.TimeMeasure;
import com.javaweb.dao.UserDao;
import com.javaweb.model.User;
import com.javaweb.service.UserService;
import com.javaweb.service.dto.PageResult;
import com.javaweb.service.dto.UserConditionDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @desc:
 * @author: Ping
 * @date: 2024/9/23 12:36
 */
@TimeMeasure(granular = GranularType.MIL_SECOND)
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao ;

    @Override
    public User login(String username, String password) {
        if (username==null){
            throw new IllegalArgumentException("username is null");
        }
        if (password==null){
            throw new IllegalArgumentException("password is null");
        }
        User user=userDao.selectOne(username, password) ;
        // 如果没有找到用户，抛出异常
        if (user == null) {
            throw new RuntimeException("用户名或密码错误");
        }
        return user;
    }

    @Override
    public List<User> findUserByName(String username) {
        User user = new User() ;
        if( user.getName().equals( "123")) {

        }
        return userDao.selectByName(username) ;
    }

    @Override
    public List<User> queryByName(String username) {
        if (username==null){
            throw new IllegalArgumentException("username is null");
        }
        List<User> users=userDao.selectByName(username) ;
        return users;
    }

    @Override
    public int register(User user) {
        if (user==null){
            throw new IllegalArgumentException("user is null");
        }
        int ret=userDao.insert(user);
        return ret>0?1:0;
    }

    @Override
    public PageResult queryUsers(UserConditionDto userCondition, Integer pageNum, Integer pageSize) {
        // 检查页码和每页大小是否有效
        if (pageNum == null || pageNum <= 0) {
            throw new IllegalArgumentException("pageNum must be greater than 0");
        }
        if (pageSize == null || pageSize <= 0) {
            throw new IllegalArgumentException("pageSize must be greater than 0");
        }

        String username = userCondition.getUsername();
        List<User> allUsers = userDao.selectByName(username); // 获取所有符合条件的用户

        // 手动实现分页
        int total = allUsers.size(); // 总记录数
        int fromIndex = (pageNum - 1) * pageSize;
        int toIndex = Math.min(fromIndex + pageSize, total);

        List<User> pagedUsers = allUsers.subList(fromIndex, toIndex); // 获取当前页的数据

        // 创建 PageResult 对象并设置数据
        PageResult<User> pageResult = new PageResult<>();
        pageResult.setData(pagedUsers);
        pageResult.setPageNum(pageNum);
        pageResult.setPageSize(pageSize);
        pageResult.setTotal(total);

        return pageResult; // 返回分页结果
    }

    @Override
    public void addAssignRole(String userId, List roleIds) {
        // 检查用户 ID 是否有效
        if (userId == null) {
            throw new IllegalArgumentException("userId is null ");
        }
        // 检查角色 ID 列表是否有效
        if (roleIds == null ) {
            throw new IllegalArgumentException("roleIds is null ");
        }
        Integer userid=Integer.parseInt(userId);
        for (Object roleId : roleIds) {
            Integer roleid=Integer.parseInt((String) roleId);
            userDao.insertUserRole(userid,roleid);//调用了userDao层中的insertUserRole()方法
        }
    }

    @Override
    public List<Integer> getRoleIdsByUserId(String userId) {
        // 检查用户 ID 是否有效
        if (userId == null) {
            throw new IllegalArgumentException("userId is null");
        }
        return userDao.getRoleIdsByUserId(Integer.parseInt(userId)); // 调用 UserDao 的方法
    }
}
